package com.itheima.sys.service.impl;

import cn.hutool.db.handler.BeanHandler;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.common.exception.NcException;
import com.itheima.common.exception.enums.ResponseEnum;
import com.itheima.common.util.BeanHelper;
import com.itheima.common.util.StringUtil;
import com.itheima.sys.dto.SysUserDTO;
import com.itheima.sys.entity.SysUser;
import com.itheima.sys.mapper.SysUserMapper;
import com.itheima.sys.service.ISysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.sys.service.SmsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.sql.NClob;
import java.util.TreeMap;

/**
 * <p>
 * 用户表 系统-用户表 服务实现类
 * </p>
 *
 * @author itheima
 * @since 2020-12-13
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public SysUserDTO querUser(String username) {
        //查询条件 用户名或者手机号  状态：可用
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysUser::getMobile, username);
        queryWrapper.lambda().eq(SysUser::getStatus, true);
        queryWrapper.lambda().or().eq(SysUser::getUsername, username);
        SysUser sysUser = this.getOne(queryWrapper);
        if (sysUser == null) {
            throw new NcException(ResponseEnum.USER_NOT_REGISTER);
        }
        return BeanHelper.copyProperties(sysUser, SysUserDTO.class);
    }

    @Override
    public Boolean checkExists(String mobile) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysUser::getMobile, mobile);
        int count = this.count(queryWrapper);
        return count > 0;
    }

    /**
     * 添加用户
     *
     * @param sysUserDTO
     * @param checkcode
     * @return 系统用户ID
     */
    @Override
    public Long register(SysUserDTO sysUserDTO, String checkcode) {
        //1.验证验证码是否正确
        String prefix = SmsService.getPrefix(2);
        String realCheckcode = redisTemplate.opsForValue().get(prefix + sysUserDTO.getMobile());
        if (StringUtils.isNotBlank(realCheckcode) && realCheckcode.equals(checkcode)) {
            //2.向系统用户sys_user表中新增记录 注意：密码需要进行Bcrypt加密
            SysUser sysUser = BeanHelper.copyProperties(sysUserDTO, SysUser.class);
            sysUser.setPassword(passwordEncoder.encode(sysUserDTO.getPassword()));
            boolean b = this.save(sysUser);
            if (!b) {
                throw new NcException(ResponseEnum.INSERT_OPERATION_FAIL);
            }
            return sysUser.getId();
        } else {
            throw new NcException(ResponseEnum.CODE_IMAGE_ERROR);
        }
    }
}
